#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

int my_rand()
{
  return (std::rand() % 1000);
};

struct less_mag : public std::binary_function<int, int, bool>
{
  bool
  operator() (int x, int y)
  {
    return (x < y);
  }
};

struct adder : public std::unary_function<int, void>
{
  int sum;

  adder() : sum(0) { }

  void
  operator() (int x)
  {
    sum += x;
  }
};

int main()
{
  std::vector<int> V(10);
  std::generate(V.begin(), V.end(), my_rand);

  std::cout << "Before sorting: " << std::endl;
  std::copy(V.begin(), V.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  std::sort(V.begin(), V.end(), less_mag());

  std::cout << "After sorting: " << std::endl;
  std::copy(V.begin(), V.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;
  
  adder result = std::for_each(V.begin(), V.end(), adder());
  std::cout << "Sum = " << result.sum << std::endl;
}
